package com.two.screen.water.balance.net.observer

import android.app.Activity
import com.two.screen.water.balance.bean.BaseBean
import com.two.screen.water.balance.utils.CommUtils
import com.two.screen.water.balance.utils.ToastUtil
import com.two.screen.water.balance.widget.dialog.LoadingDialog
import io.reactivex.disposables.Disposable

/**
 * 创建者     Noel
 * 创建时间   2018/1/20 16:55
 * 描述	       对于网络请求是否需要弹出进度对话框
 */

abstract class DialogObserver<T>(activity: Activity, mDialogTitle: String = "正在加载...") : BaseObserver<T>(activity) {

    val loadingDialog: LoadingDialog?

    init {
        loadingDialog = LoadingDialog(activity, mDialogTitle)
    }


    override fun onSubscribe(d: Disposable) {
        mDisposable = d
        if (loadingDialog != null && !loadingDialog.isShowing) {
            loadingDialog.show()
        }
    }

    override fun onError(e: Throwable) {
        CommUtils.println("onError:", e.toString())
        ToastUtil.showToast(e.message)
        if (!isHandleRequestMultiData()) {
            mDisposable!!.dispose()
        }
        /*loadingDialog!!.setCancelable(true)
        loadingDialog.setCanceledOnTouchOutside(true)*/
        if (loadingDialog != null && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }

    override fun dataIsNull(t: BaseBean<out T>) {
        super.dataIsNull(t)
        if (loadingDialog != null && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }

    override fun onComplete() {
        //网络请求结束后关闭对话框
        if (loadingDialog != null && loadingDialog.isShowing) {
            loadingDialog.dismiss()
        }
    }
}
